<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Mapa extends CI_Controller {
		
		function index(){
			$menu=$this->_menu();
			$data['main_content'] = 'mapa/mapa.html';
			$data['menu']=$menu;
			$data['current']=0;
			$this->load->view('includes/template', $data);
		}
		
		function buscar(){
			$menu=$this->_menu();
			$data['main_content'] = 'mapa/buscar.php';
			$data['menu']=$menu;
			$data['current']=1;
			$this->load->view('includes/template', $data);
		}
		
		function search_for_city($city){
			$this->load->model('Places_model');
			$result = $this->Places_model->places_for_city(urldecode($city));
			$data=array(	
					'places'		=>$result
			);
			$this->load->view('places/myPlaces', $data);
		}
		
		function search_google($lat,$long){
			require_once('googlePlaces.php');
				
			$gplaces = New GooglePlaces;
			$gplaces->SetLocation($lat.','.$long);
			$gplaces->SetRadius(10000);
			$gplaces->SetTypes("park");
			
			$results = $gplaces->Search();
						
			echo $this->_eliminarDuplicados($results);
		}
		function google_details($reference){
			require_once('googlePlaces.php');
			
			$gplaces = New GooglePlaces;
			$gplaces->SetReference($reference);
			
			$results = $gplaces->Details();
			echo json_encode($results);
		}
		
		function _eliminarDuplicados($data){
			$this->load->model('Places_model');
			$respuesta=array();
			$j=0;
			for($i=0;$i<count($data['results']);$i++){
				if($this->Places_model->exist($data['results'][$i]['id'])){
					
				}else{
					$respuesta[$j++]=$data['results'][$i];
				}
			}
			return json_encode(array('results'=>$respuesta));
		}
		
	 	function _menu(){
			$mejores = $this->_mejorPuntuacion();
			$ultimos = $this->_ultimos();
			return array(
					'mejores' =>$mejores,
					'ultimos' =>$ultimos
			);
		}
		function _mejorPuntuacion(){
			$this->load->model('Calificacion_model');	
			return $this->Calificacion_model->mejorPuntuacion();
		}
		function _ultimos(){
			$this->load->model('Places_model');
			return $this->Places_model->ultimosSitios();
		}
	}
	
?>